home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / BAG.CPP < prev    next >
C/C++ Source or Header  |  1993-04-27  |  3KB  |  140 lines

  1. #include "bag.h"
  2. #include "assocint.h"
  3. #include "integer.h"
  4.  
  5. #define THIS    Bag
  6. #define BASE    Collection
  7. //DEFINE_CLASS(THIS, BASE);
  8. DEFINE_CLASS(Bag,Collection);
  9.  
  10. Bag::Bag(unsigned size) : contents(size)
  11. {
  12.     count = 0;
  13. }
  14.  
  15. Bag::Bag(const Bag& b) : contents(b.contents)
  16. {
  17.     count = b.count;
  18. }
  19.  
  20. void Bag::operator=(const Bag& b)
  21. {
  22.     contents = b.contents;
  23.     count = b.count;
  24. }
  25.     
  26. void Bag::reSize(unsigned newSize)
  27. {
  28.     contents.reSize(newSize);
  29. }
  30.  
  31. Object* Bag::addWithOccurrences(const Object& ob, unsigned n)
  32. {
  33.     AssocInt* a = (AssocInt*)&contents.assocAt(ob);
  34.     Object* o = (Object*)&ob;
  35.     if (a==nil) contents.add(*new AssocInt(ob,n));
  36.     else {
  37.         Integer& i = *((Integer*)(a->value()));
  38.         o = a->key();
  39.         i.value(i.value()+n);
  40.     }
  41.     count += n;
  42.     return o;
  43. }
  44.  
  45. Object* Bag::add(const Object& ob)
  46. {
  47.     return addWithOccurrences(ob,1);
  48. }
  49.  
  50. Object*& Bag::at(int i) const { return contents.at(i); }
  51.  
  52. unsigned Bag::capacity() const   { return contents.capacity(); }
  53.  
  54. Collection& Bag::addContentsTo(Collection& cltn) const
  55. {
  56.     DO(*this,Object*,ob) cltn.add(*ob); DONE
  57.     return cltn;
  58. }
  59.  
  60. void Bag::deepenShallowCopy()
  61. {
  62.     BASE::deepenShallowCopy();
  63.     contents.deepenShallowCopy();
  64. }
  65.  
  66. Object* Bag::doNext(Iterator& pos) const
  67. {
  68.     AssocInt* a;
  69.     while (YES) {
  70.         if (pos.num == 0) {
  71.             a = (AssocInt*)contents.doNext(pos);
  72.             if (a == NULL) return NULL;
  73.         }
  74.         else a = (AssocInt*)contents.at(pos.index-1);
  75.         if (pos.num++ < ((Integer*)a->value())->value())
  76.             return a->key();
  77.         pos.num = 0;
  78.     }
  79. }
  80.  
  81. Object* Bag::remove(const Object& ob)
  82. {
  83.     register AssocInt* a = (AssocInt*)&contents.assocAt(ob);
  84.     Object* rob = (Object*)&ob;
  85.     if (a==nil)
  86.         //DTerror("OOPS_REMOVEERR,DEFAULT,this,className(),ob.className(),&ob",className());
  87.         DTerror("Tried to remove object not in collection: ",className());
  88.     register Integer* i = (Integer*)(a->value());
  89.     register unsigned n = i->value();
  90.     if (--n == 0) {
  91.         rob = a->key();
  92.         delete contents.remove(*a);
  93.     }
  94.     else i->value(n);
  95.     --count;
  96.     return rob;
  97. }
  98.  
  99. bool Bag::operator==(const Bag& b) const
  100. {
  101.     return count==b.count && contents==b.contents;
  102. }
  103.  
  104. unsigned Bag::hash() const    { return count^contents.hash(); }
  105.  
  106. bool Bag::isEqual(const Object& p) const
  107. {
  108.     return p.isSpecies(class_Bag) && *this==*(Bag*)&p;
  109. }
  110.  
  111. const Class* Bag::species() const { return &class_Bag; }
  112.  
  113. unsigned Bag::occurrencesOf(const Object& ob) const
  114. {
  115.     register AssocInt* a = (AssocInt*)&contents.assocAt(ob);
  116.     if (a==nil) return 0;
  117.     else return ((Integer*)a->value())->value();
  118. }
  119.  
  120. void Bag::printOn(ostream& strm) const
  121. {
  122.     unsigned n=0;
  123.     strm << className() << "[\n";
  124.     DO(contents,Object*,o)
  125.         if (n>0) strm << "\n";
  126.         o->printOn(strm);
  127.         n++;
  128.     DONE
  129.     strm << "]\n";
  130. }
  131.  
  132. unsigned Bag::size() const   { return count; }
  133.  
  134. Bag Collection::asBag() const
  135. {
  136.     Bag cltn(MAX(size(),CLTN_DEFAULT_CAPACITY));
  137.     addContentsTo(cltn);
  138.     return cltn;
  139. }
  140.